package net.uhuijia.sys.area.service.impl;

import net.uhuijia.constants.DBConst;
import net.uhuijia.enums.AreaLevel;
import net.uhuijia.sys.area.service.SysAreaService;
import net.uhuijia.vo.sys.area.AreaVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.sys.area.persistence.dao.SysAreaDao;
import net.uhuijia.sys.area.persistence.model.SysArea;
import net.uhuijia.sys.area.persistence.model.SysAreaExample;
import net.uhuijia.sys.area.persistence.model.SysAreaExample.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class SysAreaServiceImpl
     extends CrudService<SysAreaDao,SysArea,SysAreaExample> implements SysAreaService{
    @Autowired
    private SysAreaDao sysAreaDao;

    @Override
    public List<SysArea> findAreaByParentIdAndAreaLevel(String parentId, AreaLevel areaLevel) {
        SysAreaExample example = new SysAreaExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        if(StringUtils.isNotEmpty(parentId))
            criteria.andParentRegionIdEqualTo(parentId);
        criteria.andRegionLevelEqualTo(areaLevel.getLevel());
        List<SysArea> sysAreas = this.list(example);
        return sysAreas;
    }

    @Override
    public List<AreaVo> findAreaVoByParentIdAndAreaLevel(String parentId, AreaLevel areaLevel) {
        List<SysArea> sysAreas = this.findAreaByParentIdAndAreaLevel(parentId,areaLevel);
        List<AreaVo> areaVos = new ArrayList<>();
        for(SysArea sysArea : sysAreas){
            AreaVo areaVo = new AreaVo(sysArea.getId(),sysArea.getRegionName());
            areaVos.add(areaVo);
        }
        return areaVos;
    }
}